package com.kejiguan.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.kejiguan.ui.adapter.ActivationExhibitionAdapter;
import com.kejiguan.ui.dialog.CommDialogUtil;
import com.kejiguan.ui.model.ExhibitionNeItem;
import com.kejiguan.ui.presenter.ActivationExhibitionPresenter;
import com.kejiguan.ui.response.ActivationExhibitionResponse;
import com.kejiguan.ui.response.QueryUnActivationExhibitionResponse;
import com.kejiguan.ui.widget.MyLinearLayoutManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by YYY on 16/7/18.
 */
public class ActivationExhibitionActivity extends Activity implements View.OnClickListener{

    private ActivationExhibitionPresenter mPresenter;

    private RecyclerView exhibitionListView;
    private Button activationBtn,backBtn;
    private ActivationExhibitionAdapter exhibitionAdapter;
    private ExhibitionNeItem exhibitionNeItem;
    private String exhibitionAreaId,exhibitionAreaName;


    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        getIntentData();
        setContentView(R.layout.activity_activation_exhibition_layout);
        EventBus.getDefault().register(ActivationExhibitionActivity.this);
        mPresenter=new ActivationExhibitionPresenter();
        mPresenter.onCreate(ActivationExhibitionActivity.this);

        exhibitionListView= (RecyclerView) findViewById(R.id.exhibition_list);
        activationBtn= (Button) findViewById(R.id.activation_btn);
        activationBtn.setOnClickListener(this);
        backBtn= (Button) findViewById(R.id.back_btn);
        backBtn.setOnClickListener(this);

        MyLinearLayoutManager layoutManager=new MyLinearLayoutManager(ActivationExhibitionActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        exhibitionListView.setLayoutManager(layoutManager);

        exhibitionAdapter=new ActivationExhibitionAdapter(ActivationExhibitionActivity.this,null);
        exhibitionListView.setAdapter(exhibitionAdapter);

        mPresenter.queryUnActivationExhibitions();

    }

    private void getIntentData()
    {
        exhibitionAreaId=getIntent().getStringExtra("exhibitionAreaId");
        exhibitionAreaName=getIntent().getStringExtra("exhibitionAreaName");
    }


    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(QueryUnActivationExhibitionResponse response) {

        if ("0000".equals(response.getRetCode()))
        {
            exhibitionAdapter.setExhibitionNeItems(response.getUnActivationList());
        }else {
            String errorInfo= TextUtils.isEmpty(response.getRetInfo())?"查询预留展项失败":response.getRetInfo();
            Toast.makeText(ActivationExhibitionActivity.this, errorInfo, Toast.LENGTH_SHORT).show();
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ActivationExhibitionResponse response) {
        activationBtn.setClickable(true);
        if ("0000".equals(response.getRetCode()))
        {
            Toast.makeText(ActivationExhibitionActivity.this,"添加预留展项成功!",Toast.LENGTH_SHORT).show();
            finish();
        }else {
            Toast.makeText(ActivationExhibitionActivity.this, response.getRetInfo(), Toast.LENGTH_SHORT).show();
        }

    }


    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.activation_btn:
                exhibitionNeItem=exhibitionAdapter.getSelectedExhibitionNeItem();
                if (null!=exhibitionNeItem)
                {
                    CommDialogUtil.getInstance().show(ActivationExhibitionActivity.this,"确认将预留展项"+exhibitionNeItem.getExhibitionName()+"添加进"+exhibitionAreaName+"展区吗?",
                            ActivationExhibitionActivity.this,"添加","取消",0);
                }else {
                    Toast.makeText(ActivationExhibitionActivity.this,"请选择你要添加的预留展区",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.back_btn:
                finish();
                break;
            case R.id.dialog_confirm_btn:
                activationBtn.setClickable(false);
                CommDialogUtil.getInstance().dismiss();
                mPresenter.activationExhibitions(exhibitionNeItem,exhibitionAreaId);
                break;
            case R.id.dialog_cancel_btn:
                CommDialogUtil.getInstance().dismiss();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

        if (resultCode==RESULT_OK)
        {
            switch (requestCode)
            {
                case 99:
                    String ids=intent.getStringExtra("ids");
                    String exhibitionId=intent.getStringExtra("exhibitionId");
                    List<ExhibitionNeItem> exhibitionNeItems=exhibitionAdapter.getExhibitionNeItems();
                    if (null!=exhibitionNeItems)
                    {
                        for (int i=0;i<exhibitionNeItems.size();i++)
                        {
                            ExhibitionNeItem exhibitionNeItem2=exhibitionNeItems.get(i);
                            if (exhibitionNeItem2.getExhibitionId().equals(exhibitionId))
                            {
                                exhibitionNeItem2.setResource(ids);

                                break;
                            }
                        }
                    }
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, intent);
    }

}
